package com.eudon.ai.agent.advisor;

import org.springframework.ai.chat.client.advisor.api.*;
import org.springframework.core.Ordered;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;

/**
 * @author eudon
 * @description 将用户的问题重复拼接一次，然后向AI提问
 * @date 2025/8/8
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
public class R2Advisor implements CallAroundAdvisor, StreamAroundAdvisor, Ordered {
    private final int order = -2;
    private final String READ_2 = ",请再次阅读用户的问题:{}";

    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        advisedRequest = before(advisedRequest);
        AdvisedResponse advisedResponse = chain.nextAroundCall(advisedRequest);
        return advisedResponse;
    }

    private AdvisedRequest before(AdvisedRequest advisedRequest) {
        Map<String, Object> userParams = new HashMap<>(advisedRequest.userParams());
        userParams.put("user_text", advisedRequest.userText());
        return AdvisedRequest.from(advisedRequest)
                .userText("""
                        {user_text}
                        Read the question again: {user_text}
                        """)
                .userParams(userParams)
                .build();
    }

    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        return null;
    }

    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }

    @Override
    public int getOrder() {
        return this.order;
    }
}
